/**
 *	流程模块接口文件：商品管理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.jxc.jxcbase;




import com.framerock.db.DB;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.Flow.Flow;
import com.framework.ACore.Model;
import com.framework.Flow.FlowListener;
import com.framework.Model.ModelDept;
import com.framework.Model.ModelOption;

import java.util.ArrayList;
import java.util.Map;


public class FlowListenerjxcbase extends FlowListener {



    @Override
    public Boolean onModejs() {
        return true;
    }

    @Override
    public void onFlowunitadd() {
        mFlow.addOption("进销存选项-商品类型", "jxcbasetype", new String[]{
                "name=办公物品",
                "name=行政物品",
        });
        mFlow.addOption("进销存选项-商品单位", "jxcbaseunit", new String[]{
                "name=个",
                "name=只",
                "name=斤",
        });
        mFlow.addOption("进销存选项-商品入库类型", "jxcstockin", new String[]{
                "name=初始入库,value=0",
                "name=采购入库,value=1",
                "name=调拨入库,value=2",
        });
        mFlow.addOption("进销存选项-商品出库类型", "jxcstockout", new String[]{
                "name=销售出库,value=20",
                "name=报废出库,value=21",
                "name=采购退货出库,value=22",
                "name=调拨出库,value=23",
        });
    }


    @Override
    public String onDefaultOrder() {
        return "`sort` ASC";
    }


    @Override
    public ArrayList<Map<String, String>> onStore(String action, String params) {
        if(action.equals("churukustate"))return churukustate();
        return null;
    }

    private ArrayList<Map<String, String>> churukustate(){
        ArrayList<Map<String, String>> rows = Rock.getMapList();
        rows.add(Rock.getMap("name=待出入库,value=0,color=red"));
        rows.add(Rock.getMap("name=已完成,value=1,color=green"));
        rows.add(Rock.getMap("name=部分出入库,value=2,color=blue"));
        return rows;
    }

    @Override
    public String onAction(String act, String params) {
        if(act.equals("ckdata"))return this.ckdata();
        if(act.equals("reloadstock"))return this.reloadstock(mFlow.COMPANYID, params);
        return super.onAction(act, params);
    }

    //仓库数据
    private String ckdata(){
        ArrayList<Map<String, String>> rows = Model.get(this.nDb, "jxcstock").getAll("`cid`="+mFlow.COMPANYID+"", "id,name", "`sort` ASC");
        return Json.getListString(rows);
    }

    //刷新库存
    public String reloadstock(String cid, String sid)
    {
        String where = "";
        if(!Rock.isEmpt(sid)){
            mFlow.mModel.update("`stock`=0", "`id` IN("+sid+")");
            where = "AND `sid` IN("+sid+")";
        }else{
            mFlow.mModel.update("`stock`=0", "`cid`="+cid+"");
        }
        String sql = "SELECT `sid`,SUM(`count`)stock FROM `"+DB.chuliPerfix("jxcgoods")+"` WHERE `cid`="+cid+" "+where+" GROUP BY `sid`";
        ArrayList<Map<String, String>> rows = this.nDb.getAll(sql);
        if(rows != null)for(int i=0;i<rows.size();i++){
            mFlow.mModel.update("`stock`="+rows.get(i).get("stock")+"", rows.get(i).get("sid"));
        }
        return "ok";
    }

    @Override
    public void onSaveafter(Map<String, String> da, String mid, Boolean isadd) {
        String str = da.get("name");
        String str1= "";
        str1+=""+da.get("xinghao");
        str1+=""+da.get("guige");
        if(!Rock.isEmpt(str1))str+="("+str1+")";
        this.nDb.update(DB.chuliPerfix("jxcgoodn"), "`sname`='"+str+"'", "`sid`="+mid+"");
        this.nDb.update(DB.chuliPerfix("jxcgoods"), "`sname`='"+str+"'", "`sid`="+mid+"");
    }

    @Override
    public Map<String, String> onReplacers(Map<String, String> da, int lx)
    {
        double stock = Double.parseDouble(da.get("stock"));
        if(stock <= 0)da.put("stock_color", "red");
        return da;
    }

    public Map<String, String> onDataafter()
    {
        Map<String, String> da = Rock.getMap();
        if(mRequest.COMPANYBOOL && mRequest.LOADCI==1) {
            da.put("typearr", Json.getListString(ModelOption.get(this.nDb).getTreeData("jxcbasetype", mRequest.COMPANYID, "")));
        }
        return da;
    }

    @Override
    public Map<String, String> onDatabefore(){
        String typeid   = mRequest.get("typeid");
        String where    = "";
        if(!Rock.isEmpt(typeid)){
            String typeidall = ModelOption.get(this.nDb).allDownid(typeid);
            where = "`typeid` IN("+typeidall+")";
        }
        return Rock.getMap(new String[]{
                "where", where,
        });
    }
}
